﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class DataBase
    {
        private SqlConnection _DatabaseCon;

        private string connString = System.Configuration.ConfigurationManager.ConnectionStrings["binglian"].ConnectionString;

        public SqlConnection DatabaseCon
        {
            get { return _DatabaseCon; }
            set { _DatabaseCon = value; }
        }

        private SqlCommand _SQLCommand;

        public SqlCommand SQLCommand
        {
            get{ return _SQLCommand; }
            set{ _SQLCommand = value; }
        }

        private SqlDataAdapter _SDAdapter;

        public SqlDataAdapter SDAdapter
        {
            get { return _SDAdapter; }
            set { _SDAdapter = value; }
        }

        public void DatabaseConnection()
        {
            try
            {
                _DatabaseCon = new SqlConnection(this.connString);
                //_DatabaseCon = new SqlConnection("Data Source=XYHOST;Initial Catalog = ChingeDesign;User Id=22222;Password=22222");
                //_DatabaseCon = new SqlConnection("Data Source=119.148.161.39;Initial Catalog =chingedesign;User Id=chingedesign;Password=pablo");
                _DatabaseCon.Open();
            }
            catch(Exception ex) 
            {
                WriteLog(ex.ToString(), "数据库连接失败:" + this.connString);
            }  
        }

        public SqlDataReader GetdataRecords(string sqlString)
        {
            SqlDataReader Reader = null;
            try
            {
                if (_DatabaseCon == null || _DatabaseCon.State == System.Data.ConnectionState.Closed)
                {
                    DatabaseConnection();
                }
                _SQLCommand = new SqlCommand(sqlString, _DatabaseCon);
                Reader = _SQLCommand.ExecuteReader();
            }
            catch (Exception ex)  
            {
                _DatabaseCon.Close();
                WriteLog(ex.ToString(), sqlString);
            }
            return Reader;
        }

        public void CloseDB()
        {
            _DatabaseCon.Close();
        }

        public int UpdateRecords(string sqlString)
        {
            int state = -1;
            WriteLog("UpdateRecords",sqlString);
            try
            {
                if (_DatabaseCon == null || _DatabaseCon.State != System.Data.ConnectionState.Open)
                {
                    DatabaseConnection();
                }

                _SQLCommand = new SqlCommand(sqlString, _DatabaseCon);
                state = _SQLCommand.ExecuteNonQuery();
                _DatabaseCon.Close();
            }
            catch(Exception ex) 
            {
                _DatabaseCon.Close();
                WriteLog(ex.ToString(), sqlString);
            }
            return state;
        }

        private void WriteLog(string ex,string sql)
        {
            try
            {
                //string filename = System.Configuration.ConfigurationManager.ConnectionStrings["databaselog"].ConnectionString;
                string filename = AppDomain.CurrentDomain.BaseDirectory + "/log/sql" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Append, System.IO.FileAccess.Write);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
                sw.WriteLine(DateTime.Now+": ");
                sw.WriteLine("   ex:" + ex);
                sw.WriteLine("  sql:" + sql);
                sw.Close();
            }
            catch { }
        }
    }
}
